package com.st.buy.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.st.buy.manager.mapper.TbItemCatMapper;
import com.st.buy.manager.service.ItemCatService;
import com.st.buy.pojo.TbItemCat;
import com.st.buy.util.pojo.EasyUITreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 张双虎
 */
@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private TbItemCatMapper itemCatMapper;
    public List<EasyUITreeNode> queryCatByParentId(long pid) {
        List<EasyUITreeNode> nodes = new ArrayList<>();
        //准备查询条件
        QueryWrapper<TbItemCat> wrapper = new QueryWrapper<TbItemCat>();
        //条件是 paretn_id = #{pid}
        wrapper.eq("parent_id",pid);
        List<TbItemCat> itemCats = itemCatMapper.selectList(wrapper);
        //将itemCatList转换为EasyUITreeNodeList
        for (TbItemCat itemCat : itemCats) {
            EasyUITreeNode node = new EasyUITreeNode();
            node.setId(itemCat.getId());
            node.setText(itemCat.getName());
            node.setState(itemCat.getParent()?"closed":"open");
            nodes.add(node);
        }
        return nodes;
    }
}
